﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lmondeil.Crm.EF;

namespace Atlanchim.Crm.Mui.Pages.Societes
{
    public class PageListSocietesModel : AbstractPageListModel
    {
        public List<Societe> SocietesList { get; set; }

        private ETypeSociete _typeSociete;
        public ETypeSociete TypeSociete
        {
            get
            {
                return _typeSociete;
            }
            set
            {
                _typeSociete = value;
                OnPropertyChanged("TypeSociete");

                switch (_typeSociete)
                {
                    case ETypeSociete.Indefini:
                        this.Title = "Tous les Clients et tous les Prospects";
                        break;
                    case ETypeSociete.Client:
                        this.Title = "Tous les Clients";
                        break;
                    case ETypeSociete.Prospect:
                        this.Title = "Tous les Prospects";
                        break;
                    default:
                        this.Title = "Tous les Clients et tous les Prospects";
                        break;
                }
                OnPropertyChanged("Title");
            }
        }

        public string Title { get; set; }

        public PageListSocietesModel()
        {
            this.TypeSociete = ETypeSociete.Indefini; // Aucun type défini
        }

        public override void Load()
        {
            using (CrmContext context = new CrmContext())
            {

                // Chargement du type de société
                int typeSociete = -1;
                if (this.DicFragments.ContainsKey("TypeSociete"))
                {
                    int.TryParse(this.DicFragments["TypeSociete"], out typeSociete);
                    this.TypeSociete = (ETypeSociete)typeSociete;
                }

                //Chargement des sociétés
                string strTypeToCheck = string.Empty;
                switch (this.TypeSociete)
                {
                    case ETypeSociete.Indefini:
                        this.SocietesList = context
                                            .Societes
                                            .ToList();
                        break;
                    case ETypeSociete.Client:
                        strTypeToCheck = ETypeSociete.Client.ToString();
                        this.SocietesList = context
                                            .Societes
                                            .Where(societe => societe.Type == strTypeToCheck)
                                            .ToList();
                        break;
                    case ETypeSociete.Prospect:
                        strTypeToCheck = ETypeSociete.Prospect.ToString();
                        this.SocietesList = context
                                            .Societes
                                            .Where(societe => societe.Type == strTypeToCheck)
                                            .ToList();
                        break;
                    default:
                        this.SocietesList = context
                                            .Societes
                                            .ToList();
                        break;
                }
                OnPropertyChanged("SocietesList");

            }
        }
    }
}
